<template>
  <div>
    <el-row>
      <el-col :span="4">
        <div class="tree">
          <el-input placeholder="输入关键字进行过滤" v-model="filterText">
          </el-input>
          <el-tree @node-click="handler" class="filter-tree" :data="empData" :props="defaultProps"
            :filter-node-method="filterNode" ref="tree">
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20">
        <div v-if="params.device_id">
          <el-form :inline="true" :model="params">
            <el-form-item>
              <el-date-picker value-format="yyyy-MM" v-model="params.date" type="month" placeholder="选择月">
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="toSearch">搜索</el-button>
            </el-form-item>
          </el-form>
          <el-table :data="monitors" style="width: 100%">
            <el-table-column prop="d" label="日期" align="center">
            </el-table-column>
            <el-table-column prop="tsp" label="tsp" align="center">
            </el-table-column>
            <el-table-column prop="pm10" label="pm10" align="center">
            </el-table-column>
            <el-table-column prop="pm25" label="pm2.5" align="center">
            </el-table-column>
            <el-table-column prop="temperature" label="温度" align="center">
            </el-table-column>
            <el-table-column prop="humidity" label="湿度" align="center">
            </el-table-column>
            <el-table-column prop="wind_direction" label="风向" width="300">
            </el-table-column>
            <el-table-column prop="wind_speed" label="风速" align="center">
            </el-table-column>
          </el-table>
        </div>
        <div v-else style="text-align: center;color:red;line-height:50px">请选择设备进行查看</div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { get } from '@/utils/request'
export default {
  data() {
    return {
      filterText: '',
      empData: [],
      defaultProps: {
        children: 'children',
        // 节点标签对应对象属性值
        label: 'name'
      },
      params: {
        device_id: 0,
        engineer_id: 0
      },
      // 表格数据源
      monitors: []
    };
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    toSearch() {
      this.getMonitor();
    },
    handler(val, node) {
      // 判断点击的是工程还是设备 看有没有children属性 
      // console.log(val,node)
      // 如果val.children不存在 说明点击的是设备 根据设备id和对应工程id查询数据
      if (!val.children) {
        this.params.device_id = val.id;
        this.params.engineer_id = val.engineer_id;
        // 发起查询请求 
        this.getMonitor();
      }
    },
    async getMonitor() {
      let res = await get('/monitor/queryMouthData', this.params);
      // console.log(res,'获取响应');
      this.monitors = res.data;
    },
    // 过滤节点 
    filterNode(value, data) {
      // console.log(value, data)  value输入框值 data根据值过滤出来的对象
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    async getEmpData() {
      let res = await get('/dashboard/findEngineerDeviceTree');
      this.empData = res.data;
    }
  },
  created() {
    this.getEmpData();
  }
}
</script>
<style lang="scss" scoped>
.tree {
  height: calc(100vh - 60px);
  border-right: 1px solid #ededed;
  padding-right: 10px;
  margin-right: 10px;
}
</style>